<?php

namespace App\Http\Controllers\Manage;

use App\Project\Filters\PastHistoryChineseFilter;
use App\Project\Transform;
use App\Models\PastHistory;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use App\Project\Filters\PastHistoryFilter;
use App\Transformers\PastHistoryTransformer;
use App\Http\Requests\PastHistoryStoreRequest;
use App\Http\Requests\PastHistoryUpdateRequest;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Project\Scene\MedicalScene;

class PastHistoryController extends ApiController
{


    protected $scene;

    public function __construct(MedicalScene $scene)
    {
        parent::__construct();
        $this->scene = $scene;
    }
    /**
     * 既往史存储 多次提交视为更新
     * @param \App\Http\Requests\PastHistoryStoreRequest $request
     * @return mixed
     */
    public function store(PastHistoryStoreRequest $request)
    {

        if($this->scene->isWestern()){
            $input = (new PastHistoryFilter($request))->input();
        }else{
            $input = (new PastHistoryChineseFilter($request))->input();
        }


        $result = PastHistory::updateOrCreate(['record_id' => $input['record_id']], $input);

        return $this->response(['id' => $result['id']]);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        $data = null;
        $id   = $request->get('id');

        if ($id > 0) {

            $data = PastHistory::findOrFail($id);
        } elseif ($record_id = $request->get('record_id')) {

            $data = PastHistory::where(['record_id' => $record_id])->first();
        } else {
            abort(400, '缺失既往史ID，或者病历ID');
        }

        if (empty($data)) {
            throw (new ModelNotFoundException())->setModel(PastHistory::class);
        }

        return $this->item($data, new PastHistoryTransformer());
    }

    /**
     * @param \App\Http\Requests\PastHistoryUpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(PastHistoryUpdateRequest $request)
    {
        $where = [];

        if ($id = $request->input('id')) {
            $where['id'] = $id;
        } else {
            $where['record_id'] = $request->input('record_id');
        }

        if($this->scene->isWestern()){
            $input = (new PastHistoryFilter($request))->except(['id', 'record_id']);
        }else{
            $input = (new PastHistoryChineseFilter($request))->except(['id', 'record_id']);
        }

        if (!($extra = $request->input('extra'))) {
            $input['extra'] = null;
        }

        $input = (new Transform())->setArray($input)->convertArrayToJson(
            [
                'fat', 'hypertension', 'coronary_disease', 'hyperuricemia', 'hyperlipidemia', 'fatty_liver',
                'hepatitis', 'pancreatitis', 'pancreatic_operation', 'renal', 'cerebral_infarction', 'transfusion',
                'irritability', 'special_irritability', 'adverse_drug_reaction', 'thyroid', 'trauma','contagion',
                'extra', 'medication', 'other', 'operation', 'pcos'
            ]
        )->get();

        $result = PastHistory::where($where)->update($input);

        return $this->responseForUpdate($result);
    }
}
